import api from '@/common/request/'

const os = {};
let ua = '';
if (navigator && navigator.userAgent) {  
    ua = navigator.userAgent;
}else{
	ua = '';
}

os.plus = ua.match(/Html5Plus/i) ? true : false;
os.wechat = ua.match(/(MicroMessenger)\/([\d.]+)/i) ? true : false;
os.android = ua.match(/(Android);?[\s/]+([\d.]+)?/) ? true : false;
os.iphone = ua.match(/(iPhone\sOS)\s([\d_]+)/) ? true : false;
os.ipad = ua.match(/(iPad).*OS\s([\d_]+)/) ? true : false;

os.mp_wx = false
// #ifdef MP-WEIXIN
os.mp_wx = true
// #endif

os.mp = false
// #ifdef MP
os.mp = true
// #endif

os.app  = false
// #ifdef APP-PLUS
os.app = true
// #endif

os.h5  = false
// #ifdef H5
os.h5 = true
// if(!os.wechat){
// 	os.h5 = true
// }
// #endif

const state = {
	os: os,
	navHeight: '88',
	navSearchHeight: '88',
	statusHeight: uni.getSystemInfoSync().statusBarHeight / (uni.upx2px(100) / 100),
    time_diff: 0
}

const getters = {
    
}

const actions = {
	getServerTime({commit}) {
		return new Promise((resolve, reject) => {
			api.get('/common/init/getServerTime').then((res)=>{
				if(res.code == 0){
					let time_diff = res.data.server_time - Number(Date.parse(new Date()).toString().substr(0, 10))
					commit('setTimeDiff', time_diff)
					resolve()
				}
			})
		});
	},
	sendMcode({commit}, params) {
		return new Promise((resolve, reject) => {
			api.post('/common/mcode/sendMcode', params).then((res)=>{
				if(res.code == 0){
					resolve(res.data)
				}else{
					reject()
				}
			})
		});
	}
}

const mutations = {
	setTimeDiff(state, time_diff) {
		state.time_diff = time_diff
	}
}

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}
